<?php

//namespace app\master\controller;
//
//use think\Exception;
//use tree\Data;
//
//
///**
// * rbac模块
// * Class Rbac
// * @package app\master\controller
// */
//class Rbac extends Base
//{
//    /**
//     * 权限列表
//     */
//    public function index()
//    {
//        return $this->fetch();
//    }
//
//    /**
//     * 权限列表
//     * @return \think\response\Json
//     */
//    public function getPermission()
//    {
//        $data = model('Permission')->getList();
//        $list = Data::tree($data, 'permission_name', 'permission_id');
//        return $this->tableReturn(0, '成功', 0, $list);
//    }
//
//    /**
//     * 修改菜单
//     */
//    public function editPermissions()
//    {
//        $field = input('post.field');
//        $value = input('post.value');
//        $id = input('post.id');
//        if (false === model('permission')->save([$field => $value], ['permission_id' => $id])) {
//            $this->ajaxReturn(603, '修改失败');
//        }
//        $this->ajaxReturn(200, '修改成功');
//    }
//
//
//    /**
//     * 添加菜单
//     */
//    public function addPermission()
//    {
//        $this->view->engine->layout(false);
//        if (!request()->isPost()) {
//            $id = intval(input('id'));
//            $this->assign('id', $id);
//            return $this->fetch('addPermission');
//        }
//        $data['pid'] = intval(input('post.id'));
//        $data['permission_name'] = input('post.permission_name');
//        $data['rule'] = input('post.rule');
//        $data['icon'] = input('post.icon');
//        $data['order_number'] = input('post.order_number');
//        $data['status'] = input('post.status');
//        $data['type'] = input('post.type');
//
//        if (false === model('permission')->addPermission($data)) {
//            return $this->ajaxReturn(603, '添加失败');
//        };
//        return $this->ajaxReturn(200, '添加成功');
//    }
//
//}

namespace app\master\controller;

use think\Exception;
use Tree\Data;


/**
 * rbac模块
 * Class Rbac
 * @package app\master\controller
 */
class Rbac extends Base
{
    /**
     * 权限列表
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 权限列表
     * @return \think\response\Json
     */
    public function getPermission()
    {
        $data = model('Permission')->getList();
        $list = Data::tree($data, 'permission_name', 'permission_id');
        return $this->tableReturn(0, '成功', 0, $list);
    }

    /**
     * 修改菜单
     */
    public function editPermissions()
    {
        $field = input('post.field');
        $value = input('post.value');
        $id = input('post.id');
        if (false === model('permission')->save([$field => $value], ['permission_id' => $id])) {
            $this->ajaxReturn(603, '修改失败');
        }
        $this->ajaxReturn(200, '修改成功');
    }


    /**
     * 添加菜单
     */
    public function addPermission()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            $id = intval(input('id'));
            $this->assign('id', $id);
            return $this->fetch('addPermission');
        }
        $data['pid'] = intval(input('post.id'));
        $data['permission_name'] = input('post.permission_name');
        $data['rule'] = input('post.rule');
        $data['icon'] = input('post.icon');
        $data['order_number'] = input('post.order_number');
        $data['status'] = input('post.status');
        $data['type'] = input('post.type');

        if (false === model('permission')->addPermission($data)) {
            return $this->ajaxReturn(603, '添加失败');
        }
        return $this->ajaxReturn(200, '添加成功');
    }

    /**
     * 用户组
     */
    public function role()
    {
        return $this->fetch();
    }

    /**
     * 获取用户组
     */
    public function getRole()
    {
        $list = model('Role')->getList(['status' => 1]);
        return $this->tableReturn(0, '成功', 0, $list);
    }

    /**
     * 添加用户组
     * @return mixed|\think\response\Json
     */
    public function addRole()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            return $this->fetch('addRole');
        }
        $data = input('post.');
        if (false === db('role')->insert($data)) {
            return $this->ajaxReturn(603, '添加失败');
        }
        return $this->ajaxReturn(200, '添加成功');
    }

    /**
     * 修改用户组权限
     * @return mixed|\think\response\Json
     */
    public function userRole()
    {
        if (!request()->isPost()) {
            $id = input('id');
            $this->assign('role_id', $id);
            $permission = model('permission')->getList();
            $permission = Data::channelLevel($permission, 0, '&nbsp;', 'permission_id');
            $this->assign('permission', $permission);
            $roleList = db('role_permission')->where(['role_id' => $id])->column('permission_id');
            $this->assign('roleList', $roleList);
            return $this->fetch('userRole');
        }
        $permission = (input('post.permissions/a'));
        sort($permission);

        $id = input('id');
        $oldPermission = (db('role_permission')->where(['role_id' => $id])->column("permission_id"));
        sort($oldPermission);

        db('role_permission')->where(['role_id' => $id])->delete();
        $d = [];
        foreach ($permission as $v) {
            $d[] = ['role_id' => $id, 'permission_id' => $v];
        }
        db('role_permission')->insertAll($d);
        return $this->ajaxReturn(200, '修改成功', url('role'));
    }


//用户组删除
    public function removRole()
    {
        $id = input('id');
        db()->startTrans();
        try {
            db("role")
                ->alias("r")
                ->join("admin_role ar", "r.role_id =ar.role_id")
                ->join("admin a", "ar.admin_id= a.admin_id")
                ->where(["r.role_id" => $id])
                ->update(["a.status" => 3]);
            db("role")->where(["role_id" => $id])->update(["status" => 0]);
        } catch (Exception $e) {
            db()->rollback();
            errorlog($e);
            return $this->ajaxReturn(603, '操作错误');
        }
        db()->commit();
        return $this->ajaxReturn(200, '删除成功');
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function user()
    {
        return $this->fetch();
    }

    /**
     * 获取用户列表
     */
    public function userList()
    {
        $list = model('Admin')->getUser();
        return $this->tableReturn(0, '成功', 0, $list);
    }

    /**
     * 添加用户
     */
    public function addUser()
    {
        if (!request()->isPost()) {
            $this->view->engine->layout(false);
            $role_list = model('role')->getList(['status' => 1]);
            $this->assign('role', $role_list);
            return $this->fetch('addUser');
        }
        $admin['username'] = input('username');
        $admin['password'] = md5(input('password'));
        $admin_info['admin_info_name'] = input('nickname');
        $role['role_id'] = input('role_id');
        $admin['is_store'] = input('is_store');
        $store_type = input('store_type');
        if ($admin['is_store'] == 1) {
            if (empty($store_type)) return $this->ajaxReturn(603, '请选择门店类型！');
        }
        if (db('admin')->where(['status' => 1, 'username' => $admin['username']])->find()) {
            return $this->ajaxReturn(603, '账号重复');
        }

        db()->startTrans();
        try {
            //添加用户
            $admin_id = db('admin')->insertGetId($admin);

            //添加用户基本信息
            $admin_info['admin_id'] = $admin_id;
            db('admin_info')->insertGetId($admin_info);

            //分配权限
            $role['admin_id'] = $admin_id;
            db('admin_role')->insertGetId($role);
            if ($admin['is_store'] == 1) {
                $store = [
                    'store_name' => $admin_info['admin_info_name'],
                    'store_type' => $store_type,
                    'admin_id' => $admin_id,
                ];
                db('store')->insert($store);
            }
        } catch (\Exception $e) {
            db()->rollback();
            return $this->ajaxReturn(603, '添加失败');
        }
        db()->commit();
        return $this->ajaxReturn(200, '添加成功');
    }

    /**
     * 修改用户
     */
    public function editUser()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            $id = input('id');
            $user = db('admin')
                ->alias('a')
                ->join('admin_role ar', 'ar.admin_id = a.admin_id')
                ->join('admin_info ai', 'ai.admin_id = a.admin_id')
                ->join('store s', 's.admin_id = a.admin_id', 'left')
                ->where(['a.admin_id' => $id])
                ->find();
            $this->assign('user', $user);
            $this->assign('id', $id);
            $role_list = model('role')->getList(['status' => 1]);
            $this->assign('role', $role_list);
            return $this->fetch('editUser');
        }

        $admin_id = input('admin_id');
        $username = input('username');
        $nickname = input('admin_info_name');
        $role_id = input('role_id');
        $is_store = input('is_store');
        $store_type = input('store_type');
        if ($is_store == 1) {
            if (empty($store_type)) return $this->ajaxReturn(603, '请选择门店类型！');
            $store = [
                'store_name' => $nickname,
                'admin_id' => $admin_id,
                'store_type' => $store_type
            ];
            db('store')->insert($store);
        } else {
            $find = db('store')->where('admin_id', $admin_id)->find();
            if ($find) db('store')->where('admin_id', $admin_id)->delete();
        }
        db('admin')->where(['admin_id' => $admin_id])->update(['username' => $username, 'is_store' => $is_store]);
        db('admin_role')->where(['admin_id' => $admin_id])->update(['role_id' => $role_id]);
        db('admin_info')->where(['admin_id' => $admin_id])->update(['admin_info_name' => $nickname]);
        return $this->ajaxReturn(200, '修改成功');
    }

    /**
     * 删除用户
     */
    public function delUser()
    {
        $id = input('id');
        db('admin')->where(['admin_id' => $id])->update(['status' => 3]);
        return $this->ajaxReturn(200, '删除成功');
    }

}